package com.example.mybatisplusdemo.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.mybatisplusdemo.model.domain.Flight;
import com.example.mybatisplusdemo.model.domain.ItineraryRequest;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author yzy
 * @since 2025-07-07
 */
public interface FlightMapper extends BaseMapper<Flight> {
    @Select("<script>" +
            "SELECT * FROM flight WHERE id IN (" +
            "  <foreach collection='segmentQueries' item='query' separator=' UNION '>" +
            "    SELECT id FROM (${query.customSqlSegment}) AS s${query.index}" +
            "  </foreach>" +
            ")" +
            "<if test='globalWrapper != null'> AND ${globalWrapper.customSqlSegment}</if>" +
            "<if test='request.sortBy != null'>" +
            "  ORDER BY ${request.sortBy} ${request.sortOrder}" +
            "</if>" +
            "</script>")
    List<Flight> selectFlightsByItinerary(
            @Param("segmentQueries") List<QueryWrapper<Flight>> segmentQueries,
            @Param("globalWrapper") QueryWrapper<Flight> globalWrapper,
            @Param("request") ItineraryRequest request);
}